package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.vo.GoodsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.management.relation.Role;
import java.util.List;

@RequestMapping("goods")
@Controller
public class GoodsController {

    @DubboReference
    private GoodsService goodsService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("toGoods")
    public String toGoods(){
        return "admin/goods";
    }

    @RequestMapping("listGoods")
    @ResponseBody
    public Result listGoods(){
        try {
            List<GoodsVo> tbGoodsList = goodsService.listGoods();
            return Result.OK().setData(tbGoodsList);
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("updateAuditStatus")
    @ResponseBody
    public Result updateAuditStatus(@RequestParam(value = "ids")List<Long> ids,String status){
        try {
            goodsService.updateAuditStatus(ids,status);

            ids.stream().forEach(id ->{
                rocketMQTemplate.convertAndSend("item-goodsId",id);
            });

            return Result.OK();
        }catch (Exception e){
            return Result.ER();
        }
    }
}
